/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import org.evswork.getwit.smackwrap.Talk;

import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AbsListView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class HomePageWrapper implements OnClickListener, OnLongClickListener, OnScrollListener{
	GttMainActivity act;
	ViewGroup HomeLayout;
	ViewGroup SendLayout;
	
	ListView MsgList;
	
	ImageButton HomeImBtn;
	ImageButton AtImBtn;
	ImageButton TweetImBtn;
	
	public HomePageWrapper(GttMainActivity act) {
		this.act = act;
		bindView();
		notifyPreferenceChanged();
	}

	public void notifyPreferenceChanged() {
		boolean hws = PreferenceManager.getDefaultSharedPreferences(act).getBoolean("hide_when_scroll", false);
		if (hws) {
			MsgList.setOnScrollListener(this);		
		}
		else
			MsgList.setOnScrollListener(null);
	}
	
	private void bindView() {
		HomeLayout = (ViewGroup)act.findViewById(R.id.main_home_layout);
		
		MsgList = (ListView)HomeLayout.findViewById(R.id.msg_list);
		MsgList.setAdapter(new TweetListAdapter (act));
		
		SendLayout = (ViewGroup)HomeLayout.findViewById(R.id.send_layout);
		
		//ProfileImBtn = (ImageButton)HomeLayout.findViewById(R.id.imgb_pro);
		HomeImBtn = (ImageButton)HomeLayout.findViewById(R.id.imgb_home);
		AtImBtn = (ImageButton)HomeLayout.findViewById(R.id.imgb_at);
		TweetImBtn = (ImageButton)HomeLayout.findViewById(R.id.imgb_tweet);
		
		//ProfileImBtn.setOnClickListener(this);
		HomeImBtn.setOnClickListener(this);
		AtImBtn.setOnClickListener(this);
		TweetImBtn.setOnClickListener(this);
		
		//ProfileImBtn.setOnLongClickListener(this);
		HomeImBtn.setOnLongClickListener(this);
		AtImBtn.setOnLongClickListener(this);
	}
	
	public void show() {
		HomeLayout.setVisibility(View.VISIBLE);
	}
	
	public void hide() {
		HomeLayout.setVisibility(View.INVISIBLE);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		/*case R.id.imgb_pro:
			new StatusSelectDialogBuilder(act).show();
			return;*/
		case R.id.imgb_home:
			Talk.send("-ho");
			return;
		case R.id.imgb_at:
			Talk.send("-@");
			return;
		case R.id.imgb_tweet:
			act.showEditLayout(EditPageWrapper.FROM_MAIN,false);
			return;
		}
	}

	@Override
	public boolean onLongClick(View v) {
		switch (v.getId()) {
		case R.id.imgb_home:
			act.pctlWrapper.show("ho", false);
			return true;
		case R.id.imgb_at:
			act.pctlWrapper.show("@", false);
			return true;
		/*
		case R.id.imgb_pro:
			new AlertDialog.Builder(act)
				.setTitle("refresh interval?")
				.setItems(act.getResources().getStringArray(R.array.interval_time),
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Talk.send(act,String.format("-time %d", which +1));
							dialog.dismiss();
						}
					}
				)
				.show();
			return true;*/
		}
		return false;
	}

	@Override
	public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
	}

	@Override
	public void onScrollStateChanged(AbsListView lv, int status) {
		switch (status) {
		case SCROLL_STATE_TOUCH_SCROLL:
			SendLayout.setVisibility(View.GONE);
			break;
		default:
			SendLayout.setVisibility(View.VISIBLE);
		}
		
	}
}
